<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class category_com_uqcms extends control
{
    function __construct()
    {
        $this->aid = $_SESSION['admin']['aid'];
        parent::__construct();
    }
    public function index()
    {
        $cid = @$_GET['cid'] ? $_GET['cid'] : '0';
        $data = $this->db->get_all("select * from " . table("category_com") . " where pcid = " . $cid);
        if ($data) {
            $this->assign('data', $data);
        }
        $this->display();
    }
    public function edit()
    {
        $cid = isset($_GET['cid']) ? $_GET['cid'] : false;
        if ($cid) {
            $row = $this->db->get_one("select * from " . table('category_com') . " where cid = " . $cid);
            $this->assign('row', $row);
        }
        $this->assign('cats', $this->func_pcats());
        $this->display('category_com/edit');
    }
    public function edit_save()
    {
        if ($_POST) {
            $setsql['pcid'] = isset($_POST['pcid']) ? $_POST['pcid'] : '0';
            $setsql['name'] = isset($_POST['name']) ? $_POST['name'] : error_json('名称不能为空');
            $setsql['alias'] = isset($_POST['alias']) ? $_POST['alias'] : '';
            $setsql['px'] = isset($_POST['px']) ? $_POST['px'] : '0';
            $setsql['status'] = isset($_POST['status']) ? $_POST['status'] : '1';
            $cid = isset($_POST['cid']) ? $_POST['cid'] : false;
            if ($cid) {
                $query = $this->db->update(table('category_com'), $setsql, 'cid = ' . $cid);
            } else {
                $query = $this->db->add(table('category_com'), $setsql);
            }
            if ($query) {
                $data['error'] = '0';
                echo json_encode($data);
            } else {
                error_json('保存失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    public function func_pcats()
    {
        return $this->db->get_all("select * from " . table('category_com') . " where pcid = 0");
    }
    public function del()
    {
        $cid = isset($_GET['cid']) ? $_GET['cid'] : exit('CID ERROR');
        $query = $this->db->delete(table('category_com'), 'cid = ' . $cid);
        if ($query) {
            showmsg('删除成功');
        } else {
            showmsg('删除失败');
        }
    }
}